package com.marchsoft.controller;

import com.marchsoft.common.advice.CommonExceptionHandler;
import com.marchsoft.domain.AccountPass;
import com.marchsoft.service.IAccountPassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author rsw
 */
@RestController
@RequestMapping("account")
public class AccountPassController extends CommonExceptionHandler {

    @Autowired
    private IAccountPassService accountPassService;


    /**
     * 登录接口
     * @param accountPass 前台传来的javabean对象，与表字段对应
     * @return 返回boolean 是否登录成功
     * @author rsw
     */
    @RequestMapping("/login")
    public ResponseEntity<Boolean> isLogin(AccountPass accountPass, HttpSession session){
        Boolean isLogin=accountPassService.isLogin(accountPass);
        //如果登录成功将账号信息存进session
        if (isLogin){
            AccountPass accountPassNew=accountPassService.queryAccountId(accountPass);
            Map<String,Object> accountMap=new HashMap<>();
            accountMap.put("zid",accountPassNew.getZid());
            accountMap.put("account",accountPassNew.getAccount());
            accountMap.put("status",accountPassNew.getStatus());

            //将登录的账号id存进session
            session.setAttribute("accountMap",accountMap);
        }
        return ResponseEntity.ok(isLogin);
    }


}
